/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Haciendo click en el boton las lineas se dibujan y haciendo click en la lisa se muestra
 * por consola si hay una interseccion o no.
 *
 * 
 */
package pruebaColiciones;

import java.awt.*;
import java.util.LinkedList;

import java.awt.geom.Line2D;
import java.util.Iterator;

public class PanelPunteroColicionesV2 extends javax.swing.JPanel {
    private Graphics gc;
     private LinkedList<Line2D.Double> listaLineas = new LinkedList<Line2D.Double>();
     
      Point ini=new Point(10,10);
      Point fin=new Point(100,100);
      Line2D.Double linea1 = new Line2D.Double( ini.x, ini.y, fin.x, fin.y );
      Line2D.Double linea2 = new Line2D.Double( 150, 400, 70, 100 );
      Line2D.Double linea3 = new Line2D.Double( 600, 300, 300,150 );

    public PanelPunteroColicionesV2() {
        initComponents();
        this.listaLineas.add(linea1);
        this.listaLineas.add(linea2);
        this.listaLineas.add(linea3);
        
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                formMouseMoved(evt);
            }
        });

        jButton1.setBackground(new java.awt.Color(255, 0, 0));
        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("jButton2");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(291, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addGap(159, 159, 159))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addContainerGap(312, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        this.botonApretado=true;
        
        gc=getGraphics();
        gc.setXORMode(getBackground());
       
        Graphics2D g2d = ( Graphics2D ) gc;
        g2d.setPaint( new Color(0, 255, 0));//verde
         float guiones[] = { 1 };
        g2d.setStroke( new BasicStroke( 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10, guiones, 0 ) );
       
        g2d.draw(linea1 );
        g2d.draw(linea2 );
        g2d.draw(linea3 );
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked

        Line2D.Double linea = new Line2D.Double( evt.getX(),evt.getY(),evt.getX()+5,evt.getY()-5);
         
        Iterator iter= this.listaLineas.iterator();
        
        while(iter.hasNext())
        {
            Line2D.Double lin = (Line2D.Double) iter.next();
            
            if(linea.intersectsLine(lin))
            {
                System.out.println("#######Colicion");
                Graphics2D g2d = ( Graphics2D ) gc;
                g2d.setPaint( new Color(255,0,0));//verde
                g2d.draw(lin );
                break;
            }
            else
            {
                System.out.println("No hay interseccion");
                
            }
        }
        
    }//GEN-LAST:event_formMouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        System.out.println("Cantidad de lineas: "+this.listaLineas.size());
    }//GEN-LAST:event_jButton2ActionPerformed

    public boolean botonApretado=false;
    public boolean punteroCambiado=false;
    private int cont=0;
    private void formMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseMoved
       
        if(botonApretado)
        {
            Line2D.Double linea2 = new Line2D.Double( evt.getX(),evt.getY(),evt.getX()+5,evt.getY()-5);

          Iterator iter = this.listaLineas.iterator();
          
          while(iter.hasNext())
          {
               Line2D.Double lin=(Line2D.Double)iter.next();
               
               if(linea2.intersectsLine(lin))
               {
                   System.out.println("Colicion: "+this.cont);
                    cont++;
                    this.setCursor(new java.awt.Cursor(java.awt.Cursor.MOVE_CURSOR));
                    this.punteroCambiado=true;
                    break;
                   
               }
               else
               {
                   if(punteroCambiado)
                {
                    this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
                    this.punteroCambiado=false;
                    
                }
               }
               
               
          }
        }
        
        

         
        
        
        
    }//GEN-LAST:event_formMouseMoved

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    // End of variables declaration//GEN-END:variables


 @Override
    public void paintComponent(Graphics g){
     
        super.paintComponent(g);
        
//        if(this.listaLineas.size()>0)
//        {
//            for(Line2D.Double linea: this.listaLineas)
//            {
//                    Graphics2D g2d = ( Graphics2D ) gc;
//            g2d.setPaint( new Color(0, 255, 0));//verde
//             float guiones[] = { 1 };
//            g2d.setStroke( new BasicStroke( 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10, guiones, 0 ) );
//            g2d.draw(linea );
//            }
//        }
    }

    
   
    
}

